home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.08 Aug 89 / XCMD Source / HyperUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-14  |  3.6 KB  |  153 lines  |  [TEXT/KAHL]

  1. /****************************/
  2. /* HyperUtils.c                */
  3. /*                             */
  4. /* A collection of useful     */
  5. /* routines...                */
  6. /*                             */
  7. /****************************/
  8. #include    <MacTypes.h>
  9. #include    <OSUtil.h>
  10. #include    <MemoryMgr.h>
  11. #include    <FileMgr.h>
  12. #include    <ResourceMgr.h>
  13. #include    <StdFilePkg.h>
  14. #include     "HyperXCmd.h"
  15. #include     "HyperUtils.h"
  16.  
  17. void    CenterWindow( wptr )
  18.     WindowPtr    wptr;
  19. /***************************
  20. * Center a window in the current
  21. * screen port.  Note: Does not
  22. * attempt to work with multi-screen
  23. * systems.
  24. *
  25. * This code is inspired by a
  26. * similar routine written by Steve
  27. * Maller in MPW Pascal.  Thanks Steve.
  28. ***************************/
  29. {
  30.  short     hWindSize = wptr->portRect.right - wptr->portRect.left;
  31.  short    vWindSize = wptr->portRect.bottom - wptr->portRect.top;
  32.  short    hSize = wptr->portBits.bounds.right - wptr->portBits.bounds.left;
  33.  short    vSize = wptr->portBits.bounds.bottom - wptr->portBits.bounds.top;
  34.  
  35.  MoveWindow( wptr,     
  36.              ( hSize - hWindSize ) / 2, 
  37.              ( vSize - vWindSize + 20) / 2,
  38.             false
  39.         );
  40. }
  41.  
  42. void Concat( str1, str2 )
  43.     char    *str1;
  44.     char    *str2;
  45. /*****************************
  46. * Append string 2 to the end of
  47. * string 1.  Both strings are 
  48. * pascal-format strings.
  49. *
  50. * str1 must be large enough to hold
  51. * the new string and is assumed to 
  52. * be of Type Str255 (a pascal string)
  53. *****************************/
  54. {
  55.     short len1    = *str1;        /*** the number of chars in string 1    ***/
  56.     short len2    = *str2++;        /*** the number of chars in string 2    ***/
  57.     char  *temp;                /*** string pointer                     ***/
  58.     
  59.     if( len1 +len2  > 255 )
  60.         len2 = 255 - len1;
  61.         
  62.     *str1++ += len2 ;            /*** add sizes together to get new size    ***/
  63.     
  64.     temp = str1 + len1;            /*** move to the end of string 1        ***/
  65.     while( len2 ){
  66.         *temp++ = *str2++;        /*** add a char to temp and move along    ***/
  67.         --len2;                    /*** until all characters are added        ***/
  68.     }
  69.  
  70. }
  71.  
  72. void    CopyPStr( pStr1, pStr2 )
  73.     char    *pStr1;
  74.     char    *pStr2;
  75. /****************************
  76. * Copy the contents of pstr1 into
  77. * pstr2.  The strings are assumed 
  78. * to be of type STR255 (length byte
  79. * precedes data 
  80. *
  81. ****************************/
  82. {    short     i;
  83.     char    *tstr;
  84.     
  85.     tstr = pStr2;
  86.     
  87.     for( i = 0; i <= *pStr1; i++ )
  88.         *tstr++ = *pStr1++;
  89. }
  90.  
  91.  
  92. short GetFileNameToOpen( typs, typCnt,theName, theWDID )
  93.     SFTypeList    typs;
  94.     short        typCnt;
  95.     char        *theName;
  96.     short        *theWDID;
  97. /*****************************
  98. * Invokes SFOpenFile to query the 
  99. * user for the name of a file to 
  100. * open. 
  101. *
  102. * In:     List of types of files to
  103. *        filter for (up to 4)
  104. *
  105. * Out:    fileName if picked in theName
  106. *        working directory in theWDID
  107. *        nil otherwise
  108. *        the file's volum ref num.
  109. *
  110. * ( Note that the space for the 
  111. * string must be allocated by the
  112. * caller).
  113. *****************************/
  114. {
  115.     Point        where;
  116.     char        prompt[1];
  117.     SFReply        reply;
  118.     GrafPort    *oldPort;
  119.     WindowPtr    dlogID;
  120.     
  121.     prompt[0]         = '\0';
  122.     
  123.     /*** Get and put up the standard file    ***/
  124.     /*** dialog.  You will only see the    file***/
  125.     /*** types that you filtered for.  If     ***/
  126.     /*** you filtered for no files, then     ***/
  127.     /*** all files will display                ***/
  128.     
  129.     GetPort( &oldPort );
  130.     dlogID = GetNewDialog( (short)getDlgID, (Ptr)NIL, (Ptr)UPFRONT );
  131.     
  132.     SetPort( dlogID );
  133.     CenterWindow( dlogID );
  134.     where.h = dlogID->portRect.left;
  135.     where.v = dlogID->portRect.top;
  136.     LocalToGlobal( &where );
  137.     
  138.     SFGetFile( where, prompt, (Ptr)NIL, typCnt, typs, (Ptr)NIL, &reply );
  139.     
  140.     DisposDialog( dlogID );
  141.     SetPort( oldPort );
  142.     
  143.     /*** If the user selected a file, let's ***/
  144.     /*** get the information about it        ***/
  145.     
  146.     if (reply.good){
  147.         *theWDID = reply.vRefNum;
  148.         PtoCstr( (char *)&reply.fName );
  149.         strcpy( theName, &reply.fName  );
  150.     }
  151.     return( reply.good );
  152. }
  153.